package com.jcl.recruit.api.attachment.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.core.utils.HttpRequest;
import com.jcl.recruit.attach.service.AttachmentService;
import com.jcl.recruit.attach.vo.AttachmentPhotoVo;
import com.jcl.recruit.core.utils.DownloadFilesUtils;
import com.jcl.recruit.core.web.utils.RequestUtils;
import com.jcl.recruit.wechat.logic.WechatCommonLogic;
import com.jcl.recruit.wechat.utils.WechatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UploadAttachment
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/23 17:01
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/attachment")
public class AttachmentController {

    @Autowired
    private WechatCommonLogic wechatCommonLogic;

    @Autowired
    private AttachmentService attachmentService;

    @RequestMapping(value = "/unset/img/get/{id}", method = RequestMethod.GET)
    public void download(@PathVariable("id") String id){
        byte[] b = attachmentService.headByte(id);
        DownloadFilesUtils.downloadImg(b);
    }

    @RequestMapping(value = "/save/test", method = RequestMethod.POST)
    public ApiResponse saveTest(){
        WechatUtils.WechatUserInfo userInfo = new WechatUtils.WechatUserInfo();
        userInfo.setOpenid("ojnGKw_bdseaoQU_RWfo-LaZmuaE");
        userInfo.setHeadimgurl("http://thirdwx.qlogo.cn/mmopen/vi_32/6W4l7KQrJBCaT2OQEibSbWrrBApanyLEgohUDOtkxyqOibiaHSvO8yIpBeeCsEuAibolH92JGicJTWhpevHkTDTUEOA/132");
        Integer userId = Integer.valueOf(RequestUtils.getUserId());

        byte[] b = HttpRequest.getByte(userInfo.getHeadimgurl());

        AttachmentPhotoVo vo = new AttachmentPhotoVo();
        vo.setBytes(b);
        vo.setFileName(userInfo.getOpenid());

        wechatCommonLogic.updateUserHead(vo, userId, RequestUtils.getCorpCode());
        return ApiResponse.success(null);
    }
}
